package com.nicy.dream.utils;

/**
 * @description:升级版本redis分布式锁
 * @projectName:dream
 * @see:com.nicy.dream.utils
 * @author:NicyYe
 * @createTime:2021/2/5 13:45
 * @version:1.0
 */

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Slf4j
@Component
public class RedisLockTemplate {

    /**
    @Autowired
    private RedisDistributedLock redisDistributedLock;
    **/

    /**
     * redis分布式锁
     * @param key                    锁名
     * @param expireMS               锁的生命周期，单位毫秒
     * @param redisLockCallback      callback方法
     * @return
     */
    //public Result execute(String key,long expireMS,RedisLockCallback redisLockCallback){
      // return execute(key,expireMS,redisLockCallback,false,2);
    //}

    /**
     * 分布式锁控制
     * @param key
     * @param expireMS
     * @param redisLockCallback
     * @param isAutoReleaseLock
     * @param retryTimes
     * @return
     */
    /**
    private Result execute(String key, long expireMS, RedisLockCallback redisLockCallback, boolean isAutoReleaseLock, int retryTimes) {
        log.info("redis分布式锁控制 key={}",key);
        if(StringUtils.isEmpty(key)){
            log.info("try lock failure:key is null");
            return null;
        }
        boolean lock = redisDistributedLock.lock(key,expireMS,retryTimes);
        if(lock){
            try{
                Result o =  redisLockCallback.doInRedisLock();
                return o;
            }finally {
                if(isAutoReleaseLock){
                    redisDistributedLock.releaseLock(key);
                }
            }
        }else{
            log.info("###key已存在，终止 key={}",key);
            return Result.error("请不要重复提交数据");
        }
    }
    */

}
